package com.darwin.darwinUI.controls
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.filters.DropShadowFilter;
	import flash.utils.Dictionary;

	/**
	 * 按钮 
	 * @author vanCopper
	 * 
	 */	
	public class Button extends ButtonBase
	{
		public function Button()
		{
			super();
		}
		
		/**
		 * 鼠标移入 
		 * 
		 */		
		override public function onMouseOut():void
		{
			super.onMouseOut();
			if(background is MovieClip)
			{
				MovieClip(background).gotoAndStop(_frameDic["up"]);
			}
		}
		
		/**
		 * 鼠标移出 
		 * 
		 */		
		override public function onMouseOver():void
		{
			super.onMouseOver();
			if(background is MovieClip)
			{
				MovieClip(background).gotoAndStop(_frameDic["over"]);
			}
		}
		
		/**
		 * 鼠标弹起 
		 * 
		 */		
		override public function onMouseUp():void
		{
			super.onMouseUp();
			if(background is MovieClip)
			{
				MovieClip(background).gotoAndStop(_frameDic["over"]);
			}
		}
		
		/**
		 * 鼠标按下 
		 * 
		 */		
		override public function onMouseDown():void
		{
			super.onMouseDown();
			if(background is MovieClip)
			{
				MovieClip(background).gotoAndStop(_frameDic["down"]);
			}
		}
		
		/**
		 * 鼠标单击 
		 * 
		 */		
		override public function onClick():void
		{
			super.onClick();
		}
		
		/**
		 * 按钮皮肤 
		 * 1.如果按钮皮肤是MC 那么用帧标签或者帧数来标识按钮状态对应皮肤
		 * 	  弹起："up"(1) 指针经过："over"(2) 按下:"down"(3)
		 * 2.如果按钮皮肤是bitmap 应按照约定设置backgroundSkin字符串
		 * @param value
		 * 
		 */		
		override public function set background(value:DisplayObject):void
		{
			super.background = value;
			if(background)
			{
				if(background is Bitmap)
				{
					//TODO 位图皮肤
				}
				else if(background is MovieClip)
				{
					dealFrameDic(background as MovieClip);
					(background as MovieClip).gotoAndStop(_frameDic["up"]);
				}
			}
		}
		
		private var _frameDic:Dictionary = new Dictionary();
		private function dealFrameDic(mc:MovieClip):void
		{
			// up over down
			if(mc.totalFrames == 1)
			{
				_frameDic["up"] = 1;
				_frameDic["down"] = 1;
				_frameDic["over"] = 1;
				return;
			}
			
			var frameLabelV:Array = mc.currentLabels;
			if(frameLabelV == null || frameLabelV.length == 0)
			{
				_frameDic["up"] = 1;
				_frameDic["over"] = 2;
				_frameDic["down"] = 3;
				return;
			}
			
			_frameDic["up"] = (checkFrameLabel("up"))?"up":1;
			_frameDic["over"] = (checkFrameLabel("over"))?"over":2;
			_frameDic["down"] = (checkFrameLabel("down"))?"down":3;
			
			function checkFrameLabel(fname:String):Boolean
			{
				var isHas:Boolean;
				for (var i:int = 0; i < frameLabelV.length; i++) 
				{
					var frameLabel:FrameLabel = frameLabelV[i];
					if(frameLabel.name == fname)
					{
						isHas = true;
						break;
					}
				}
				return isHas;
			}
		}
		
		/**
		 * 设置背景皮肤资源信息字符串
		 * @param value
		 * 
		 */		
		override public function set backgroundSkin(value:String):void
		{
			super.backgroundSkin = value;
		}
		
		override protected function initDefaultSkin():void
		{
			var sp:Shape = new Shape();
			sp.graphics.beginFill(0xE5E5E5);
			sp.graphics.drawRoundRect(0,0,50,25,5,5);
			sp.graphics.endFill();
			var drpFilter:DropShadowFilter = new DropShadowFilter(1, 45, 0x000000, 1, 1, 1, .3, 1, false);
			sp.filters = [drpFilter];
			background = sp;
		}
		
	}
}